package com.study.service.impl;

import com.study.dao.UserMapper;
import com.study.pojo.entity.Userinfo;
import com.study.pojo.resp.ResultMessage;
import com.study.service.UserService;
import org.apache.catalina.security.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

/**
 * 用户模块业务逻辑接口实现类
 * @author Screncea
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    /**
     * 用户登录模块
     * @param userName 用户名
     * @param password 密码
     * @return
     */
    @Override
    public ResultMessage userLogin(String userName, String password, HttpServletResponse response) {
        Userinfo user = userMapper.findUser(userName, password);
        if (user!=null){
            Cookie cookie =new Cookie("userId",user.getUserId());
            // 一天的有效期
            cookie.setMaxAge(60*60*24);
            // 添加cookie
            response.addCookie(cookie);
            return new ResultMessage().SUCCESS();
        }

        return new ResultMessage().ERROR();
    }
}
